﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Alerter;
using DevExpress.XtraBars.Docking;
using DevExpress.XtraEditors;
using DXApplication4.渲染;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DisplayUI;
using ESRI.ArcGIS.Geometry;

namespace DXApplication4
{
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        ILayer selectLayer = null;
        int selectLayerIndex = -1;
        DataTable dataTable;
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            InitializeComponent();
            axTOCControl1.SetBuddyControl(axMapControl1);
            // This line of code is generated by Data Source Configuration Wizard
            // Fill a ExcelDataSource
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//打开栅格或矢量数据
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "shapefile文件|*.shp|TIFF格式文件|*.tif|IMAGING Image|*.img";
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK) {
                foreach (var name in openFileDialog.FileNames) {
                    if (name.Split('.')[1] == "shp")
                    {
                        IFeatureLayer pFeaturelayer = null;
                        try
                        {
                            GISdataManager.readSHP(name, ref pFeaturelayer);
                            axMapControl1.AddLayer(pFeaturelayer as ILayer, 0);
                        }
                        catch
                        {
                            XtraMessageBox.Show(string.Format("读取{0}图层失败", name));
                        }
                    }
                    else {
                        IRasterLayer pRasterLayer = null;
                        try
                        {
                            GISdataManager.readRaster(name, ref pRasterLayer);
                            axMapControl1.AddLayer(pRasterLayer as ILayer, 0);
                        }
                        catch {
                            XtraMessageBox.Show(string.Format("读取{0}图层失败", name));
                        }
                    }
                }
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//打开mxd文档
        {
            OpenFileDialog xjMxdOpenFileDialog = new OpenFileDialog();
            xjMxdOpenFileDialog.Filter = "地图文档(*.mxd)|*.mxd";


            if (xjMxdOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                string xjmxdFilePath = xjMxdOpenFileDialog.FileName;
                if (axMapControl1.CheckMxFile(xjmxdFilePath))
                {
                    axMapControl1.Map.ClearLayers();
                    axMapControl1.LoadMxFile(xjmxdFilePath);
                }
            }
            axMapControl1.ActiveView.Refresh();
        }

        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)//实现按住鼠标中间拖动，按住鼠标右键框选要素
        {

            if (e.button == 4)
            {
                this.Cursor = System.Windows.Forms.Cursors.Hand;
                axMapControl1.Pan();

            }
            if (e.button == 2) {
                this.Cursor = System.Windows.Forms.Cursors.Cross;
                IEnvelope pEnv = axMapControl1.TrackRectangle();
                axMapControl1.Map.SelectByShape(pEnv, null, false);
                axMapControl1.Refresh();
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }

        private void axMapControl1_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
        {
            this.Cursor = System.Windows.Forms.Cursors.Arrow;
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//保存mxd文档
        {
            IMxdContents pMxdC = null;
            try
            {
                pMxdC = axMapControl1.Map as IMxdContents;
                IMapDocument pMapDocument = new MapDocumentClass();
                if (axMapControl1.DocumentFilename==null)
                {
                    XtraMessageBox.Show("地图文档不存在，需新建");
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "地图文档(*.mxd)|*.mxd";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        pMapDocument.New(saveFileDialog.FileName);
                    }
                    else
                    {
                        return;
                    }
                }
                else {
                    pMapDocument.Open(axMapControl1.DocumentFilename, "");
                }
                pMapDocument.ReplaceContents(pMxdC);
                pMapDocument.Save(true, true);
                XtraMessageBox.Show("保存成功");
            }
            catch { 
            }
        }

        private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)//事件图层列表框（TOCControl）的右键弹出框
        {
            if (e.button == 2) {
                ILayer pLayer = null;
                IBasicMap pBasicMap = null;
                object plegendGroup = null;
                object pIndex = null;
                esriTOCControlItem pTocItem = esriTOCControlItem.esriTOCControlItemNone;
                axTOCControl1.HitTest(e.x, e.y, ref pTocItem, ref pBasicMap, ref pLayer,
                    ref plegendGroup,ref pIndex);
                selectLayerIndex = Convert.ToInt16(pIndex);
                Console.WriteLine(selectLayerIndex);
                if (pTocItem == esriTOCControlItem.esriTOCControlItemLayer) {
                    if (pLayer != null) { selectLayer = pLayer; }
                    popupMenu1.ShowPopup(axTOCControl1.PointToScreen(new System.Drawing.Point(e.x, e.y)));
                }
                else if (pTocItem == esriTOCControlItem.esriTOCControlItemMap) {
                    popupMenu2.ShowPopup(axTOCControl1.PointToScreen(new System.Drawing.Point(e.x, e.y)));
                }

            }
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//Layer右键弹出框中的缩放地图至图层功能
        {
            if (selectLayer != null) {
                axMapControl1.Extent = selectLayer.AreaOfInterest;
            }
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//Layer右键弹出框中的删除图层功能
        {
            if (selectLayer != null) {
                axMapControl1.Map.DeleteLayer(selectLayer as ILayer);
            }
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//Map右键弹出框中的删除所有图层功能
        {
            for (int i = axMapControl1.LayerCount-1; i >=0; i--)
            { 
                //axMapControl1.get_Layer(i).
                axMapControl1.DeleteLayer(i);
            }
        }

        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)//Layer右键弹出框中的图层渲染功能
        {
            if (selectLayer is IRasterLayer)
            {
                RasterRender render = new RasterRender(axMapControl1, axTOCControl1, selectLayer as IRasterLayer);
                render.ShowDialog();
            }
            else {
                FeatureRender render = new FeatureRender(axMapControl1, axTOCControl1, selectLayer as IFeatureLayer);
                render.ShowDialog();
            }
        }

        private void axTOCControl1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)//双击legend修改symbol
        {
            ILayer pLayer = null;
            IBasicMap pBasicMap = null;
            object plegendGroup = null;
            object pIndex = null;
            esriTOCControlItem pTocItem = esriTOCControlItem.esriTOCControlItemNone;
            axTOCControl1.HitTest(e.x, e.y, ref pTocItem, ref pBasicMap, ref pLayer,
                ref plegendGroup, ref pIndex);
            if (pTocItem == esriTOCControlItem.esriTOCControlItemLegendClass) {
                ILegendClass legendClass = (plegendGroup as ILegendGroup).get_Class((int)pIndex);
                if (!((pLayer is IRasterLayer &&
                    (pLayer as IRasterLayer).Renderer is IRasterStretchColorRampRenderer)||
                    (pLayer is IFeatureLayer &&
                    (pLayer as IGeoFeatureLayer).Renderer is IProportionalSymbolRenderer)))
                {
                    ISymbolSelector selector = new SymbolSelectorClass();
                    selector.AddSymbol(legendClass.Symbol);
                    if (selector.SelectSymbol(0) == true)
                    {
                        legendClass.Symbol = selector.GetSymbolAt(0);
                    }
                    axMapControl1.Refresh();
                    axTOCControl1.Update();
                }
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selectLayer is IFeatureLayer)
            {
                FeatureTable table=new FeatureTable(selectLayer as IFeatureLayer);
                dataTable = table.get_dataTable();
                gridView1.Columns.Clear();
                gridControl1.DataSource = dataTable;
                dockPanel1.Visibility = DockVisibility.Visible;
            }
        }

        private void barButtonItem13_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            IDW idw = new IDW();
            idw.Show(this);
        }


    }
}